﻿import os,re,urlparse

url='http://bog.csdn.net/fds/gfhgj/中文/dfg/dsf?SDfgfdg'
S_decode_list=['utf-8', 'gb2312', 'gbk', 'gb18030', 'cp1252', ]
def decode_data(data):
    for coding in S_decode_list:
        try:
            data=data.decode(coding)
            return data,coding
        except:
            pass
    return u'',''
def split_path(path,split_len=4):
    upath=decode_data(path)[0]
    valid_path=upath.replace('/','').replace('.','')
    vp_list=list(valid_path)
    if len(vp_list)>split_len:
        vp_list=vp_list[:split_len]
    return_path=''
    for c in vp_list:
        return_path=return_path+'/'+c
    return_path+='/'+path
    return_path=return_path.encode('utf-8','ignore')
    return return_path

def url2path(url):
    url=url.lower()
    if len(re.findall('/',url))==2:         #使http://www.csdn.net变成http://www.csdn.net/
        url=url+'/'
    url_parse=urlparse.urlparse(url)
    curl=url_parse[1]+url_parse[2]
    url_suffix=os.path.basename(curl)
    patt='([^/]+)/'
    path=''
    for part_url in re.findall(patt,curl):
        path+=split_path(part_url)
    path=path+'/'+url_suffix
    return path.strip()
print url
print url2path(url)



